package com.dhcc.sdc.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dto.SdcCdssItemStageDto;
import com.dhcc.sdc.base.service.ISdcStageRelationService;
import com.dhcc.sdc.cdss.dao.SdcCdssItemStageDicMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssItemStageMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssItemStageDicEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssItemStageEntity;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SdcStageRelationServiceImpl
 * @Description 诊疗阶段和诊疗方案关联表表数据服务层接口实现
 * @Author caikunshen
 * @Date 2024/6/19
 */
@Service("sdcStageRelationService")
public class SdcStageRelationServiceImpl extends BaseServiceImpl<SdcCdssItemStageMapper, SdcCdssItemStageEntity> implements ISdcStageRelationService {

    @Autowired
    private SdsQcentityMapper sdsQcentityMapper;
    @Autowired
    private SdcCdssItemStageDicMapper sdcCdssItemStageDicMapper;

    @Override
    public JSONArray tree() {
        JSONArray officeArry = new JSONArray();
        // 添加病种根节点
        List<SdsQcentityEntity> sdsQcentityEntities = sdsQcentityMapper.selectList(null);
        for (SdsQcentityEntity sdsQcentityEntity : sdsQcentityEntities) {
            JSONObject obj = new JSONObject();
            obj.put("id", "e" + sdsQcentityEntity.getId());
            obj.put("pId", 0L);
            obj.put("name", sdsQcentityEntity.getBtDesc());
            obj.put("open", true);
            obj.put("isParent", true);
            officeArry.add(obj);
        }
        // 添加诊疗阶段二级节点
        List<SdcCdssItemStageDicEntity> sdcCdssItemStageDicEntities = sdcCdssItemStageDicMapper.selectList(null);
        for (SdcCdssItemStageDicEntity sdcCdssItemStageDicEntity : sdcCdssItemStageDicEntities) {
            JSONObject obj = new JSONObject();
            obj.put("id", sdcCdssItemStageDicEntity.getId());
            obj.put("pId", "e" + sdcCdssItemStageDicEntity.getEntityId());
            obj.put("name", sdcCdssItemStageDicEntity.getStageName());
            obj.put("open", false);
            obj.put("isParent", false);
            officeArry.add(obj);
        }

        return officeArry;
    }

    @Override
    public List<SdcCdssItemStageDto> page(Page<SdcCdssItemStageDto> page, Map<String, Object> params) {
        List<SdcCdssItemStageDto> result = new ArrayList<>();
        if ("all".equals(params.get("compare"))) {
            // 获取病种对应的所有诊疗方案
            if (CommonUtil.isEmpty(params.get("qcentityId"))) {
                return new ArrayList<>();
            }
            if (CommonUtil.isEmpty(params.get("stageId"))) {
                return new ArrayList<>();
            }
            List<SdcCdssItemStageDto> sdcCdssItemStageDtos = this.baseMapper.allTreatPlanPage(page, params);
            for (SdcCdssItemStageDto sdcCdssItemStageDto : sdcCdssItemStageDtos) {
                if (CommonUtil.isNotEmpty(sdcCdssItemStageDto.getId())) {
                    sdcCdssItemStageDto.setChecked(1);
                }
            }
            result = sdcCdssItemStageDtos;
        } else if ("noMatch".equals(params.get("compare"))) {
            // 获取病种阶段对应的未关联诊疗方案
            if (CommonUtil.isEmpty(params.get("qcentityId"))) {
                return new ArrayList<>();
            }
            if (CommonUtil.isEmpty(params.get("stageId"))) {
                return new ArrayList<>();
            }
            result = this.baseMapper.noMatchTreatPlanPage(page, params);
        } else if ("match".equals(params.get("compare"))) {
            result = this.baseMapper.treatPlanPage(page, params);
        }
        for (SdcCdssItemStageDto sdcCdssItemStageDto : result) {
            switch (sdcCdssItemStageDto.getItemType()) {
                case 1:
                    sdcCdssItemStageDto.setItemTypeName("评估");
                    break;
                case 2:
                    sdcCdssItemStageDto.setItemTypeName("检查");
                    break;
                case 3:
                    sdcCdssItemStageDto.setItemTypeName("检验");
                    break;
                case 4:
                    sdcCdssItemStageDto.setItemTypeName("治疗方案");
                    break;
                case 5:
                    sdcCdssItemStageDto.setItemTypeName("护理");
                    break;
            }
        }
        return result;
    }

    @Override
    public void addRelation(Map<String, Object> map) {
        SdcCdssItemStageEntity sdcCdssItemStageEntity = new SdcCdssItemStageEntity();
        sdcCdssItemStageEntity.setItemId(Long.parseLong(map.get("treatPlanId").toString()));
        sdcCdssItemStageEntity.setStageId(Long.parseLong(map.get("stageId").toString()));
        SdcCdssItemStageEntity sdcCdssItemStage = this.baseMapper.selectOne(sdcCdssItemStageEntity);
        if (sdcCdssItemStage != null) {
            throw new BizException("不能重复添加诊疗方案");
        }
        sdcCdssItemStageEntity.insert();
    }

}
